<?php  
class ControllerModuleLast extends Controller {
	protected function index() {
		$this->language->load('module/last');
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_empty'] = $this->language->get('text_empty');					
		$this->load->model('catalog/new');
		$this->load->model('catalog/article');
		
		$this->data['news'] = array();
				
		$news = $this->model_catalog_new->getNews(0);
		$max=10;
		foreach ($news as $new) {
			$children_data = array();
			$articleslast=array();	
			$children = $this->model_catalog_new->getNews($new['new_id']);
			
			$limit=count($children)>0?$max/count($children):$max;
			foreach ($children as $child) {
				$data = array(
					'filter_new_id'  => $child['new_id'],
					'filter_sub_new' => true,
					'sort_data'		 =>'p.date_added',
					'order'              =>'ASC',
					'start'              => 0,
					'limit'              => $limit
				);		
					
				$article_total = $this->model_catalog_article->getTotalArticles($data);
							
				$children_data[] = array(
					'new_id' 	  => $child['new_id'],
					'name'        => $child['name'],
					'href'        => $this->url->link('product/new', 'newid=' . $new['new_id'] . '_' . $child['new_id'])	
				);
				if($article_total>0){
					$articles=$this->model_catalog_article->getArticles($data);
					foreach ($articles as $article){
						if ($article['image']) {
							$image = $this->model_tool_image->resize($article['image'], $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
						} else {
							$image = false;
						}
						
									
						$articleslast[] = array(
							'article_id' => $article['article_id'],
							'thumb'   	 => $image,
							'name'    	 => $article['name'],
							'description' => mb_substr(strip_tags(html_entity_decode($article['meta_description'], ENT_QUOTES, 'UTF-8')), 0, 300) . '..',
							'href'    	 => $this->url->link('product/article', 'article_id=' . $article['article_id'])
						);
					}
				}
									
			}
			
			$data = array(
				'filter_new_id'  => $new['new_id'],
				'filter_sub_new' => true,
				'sort_data'		 =>'p.date_added',
				'order'              =>'ASC',
				'start'              => 0,
				'limit'              => $limit
			);		
			$article_total = $this->model_catalog_article->getTotalArticles($data);
			if($article_total>0){
				$articles=$this->model_catalog_article->getArticles($data);
				foreach ($articles as $article){
					if ($article['image']) {
						$image = $this->model_tool_image->resize($article['image'], $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
						$image_1 = $this->model_tool_image->resize($article['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
					} else {
						$image = false;
						$image_1=false;
					}
					
								
					$articleslast[] = array(
						'article_id' => $article['article_id'],
						'thumb'   	 => $image,
						'image'		 =>$image_1,
						'name'    	 => $article['name'],
						'description' => mb_substr(strip_tags(html_entity_decode($article['meta_description'], ENT_QUOTES, 'UTF-8')), 0, 300) . '..',
						'href'    	 => $this->url->link('product/article', 'article_id=' . $article['article_id'])
					);
				}
			}	
			
			$this->data['chk_articles']=$articleslast;		
			$this->data['news'][] = array(
				'new_id' => $new['new_id'],
				'name'        => $new['name'],
				'children'    => $children_data,
				'href'        => $this->url->link('product/new', 'newid=' . $new['new_id']),
				'articles'	  =>$articleslast
			);
			
		}
		
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/Last.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/last.tpl';
		} else {
			$this->template = 'default/template/module/last.tpl';
		}
		
		$this->render();
  	}
}
?>